//
//  HuaFeiChongZhiViewController.m
//  ShangChengMoBan
//
//  Created by 张铁军 on 16/8/24.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "HuaFeiChongZhiViewController.h"
#import "QM_MobileItem.h"
#import "JieSuanView.h"
#import "AddressViewController.h"

@interface HuaFeiChongZhiViewController ()<UIActionSheetDelegate,UITableViewDataSource,UITableViewDelegate,HFYEViewDelegate,UITextFieldDelegate>
{
    NSArray *_chongZhiMZAry;
    QM_MobileItem *_mobileItem;
    UILabel *liuLiang;
    UIButton *liuLiangbtn;
    UILabel *hufFei;
    NSMutableArray *_huaFeiBtnAry;
    int _qinQiuBS; //1:可请求,0.不可请求
    NSMutableArray *_nameLabelLabelAry;
    NSMutableArray *_detailLabelAry;
    NSMutableArray *_youHuiImageAry;
    
    NSDictionary *_chongZhiDic;
    NSArray *_detailMoneyAry;
    int _biaoJi;
    
    UITableView *_recordTableView;
    
    NSArray *_recordDataAry;
    
    JieSuanView *_hfczView;
    NSDictionary *_phoneInfo;
    
    float _yuEJianMoney;//总余额减免值
    float _myCash;
    UIScrollView *_myScorllview;
    UILabel *_shouJiHaoYouwu;
    UITextField *_phoneTextField;
    UILabel *guiShuDiLabel;
}


@end

@implementation HuaFeiChongZhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [TCUtils showLoadingWithView:self.view];
//    [self duoBaoBiChaXunanBlock:^(NSDictionary * count) {
//        [TCUtils hiddonLoadingWithView:self.view];
//        
//        _myCash = [[count objectForKey:@"cash"]floatValue];
//    }];
    
    
    //    [self defaultSaveChongZhiPhone:@[@"130 6757 1523",@"133 7286 3025"]];
    _yuEJianMoney = 0;
    _recordDataAry = [self defaultChongZhiPhone];
    _biaoJi = 0;
    _qinQiuBS = 1;
    _huaFeiBtnAry = [[NSMutableArray alloc]init];
    _nameLabelLabelAry = [[NSMutableArray alloc]init];
    _detailLabelAry = [[NSMutableArray alloc]init];
    _youHuiImageAry = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.
    _chongZhiMZAry = @[@"30元",@"50元",@"100元",@"200元",@"300元",@"500元"];
    [self drawNavWithTitle:@"手机充值"];
    [self drawBackButton];
    [self drawDingDanView];
    [self drawView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(huaFeiChongZhiChengGong:) name:@"ShouJihuaFeiChongZhi" object:nil];
    
}
-(void)drawDingDanView{
    UIButton *xuZhiBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 40, 30, 25, 25)];
    [xuZhiBtn addTarget:self action:@selector(dDBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [xuZhiBtn setImage:[UIImage imageNamed:@"充值记录"] forState:UIControlStateNormal];
    [self.view addSubview:xuZhiBtn];
}

-(void)dDBtnClicked{
//    if ([self jianCeLogin]) {
//        ChonZhiJLViewController *chonZhiView = [[ChonZhiJLViewController alloc]init];
//        [self.navigationController pushViewController:chonZhiView animated:YES];
//    }
}


-(void)drawView{
    
    self.view.backgroundColor = FenLeiBeiJingHuiSe;
    _myScorllview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight - 64)];
    _myScorllview.backgroundColor = FenLeiBeiJingHuiSe;
    [self.view addSubview:_myScorllview];
    
    _shouJiHaoYouwu = [[UILabel alloc]initWithFrame:CGRectMake(20 * _kscW, 0, KScreenWidth, 30 * _kscH)];
    _shouJiHaoYouwu.text = @"手机号有误";
    _shouJiHaoYouwu.textColor = [UIColor redColor];
    _shouJiHaoYouwu.hidden = YES;
    _shouJiHaoYouwu.font = [UIFont systemFontOfSize:14];
    [_myScorllview addSubview:_shouJiHaoYouwu];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 30 * _kscH, KScreenWidth, 70 * _kscH)];
    view.backgroundColor = [UIColor whiteColor];
    [_myScorllview addSubview:view];
    
    UIButton * _tongXunLuBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30 * _kscW, 30 * _kscW)];
    [_tongXunLuBtn setImage:[UIImage imageNamed:@"cz_contacts" ] forState:UIControlStateNormal];
    [_tongXunLuBtn addTarget:self action:@selector(contactsClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(20 * _kscW, 10 * _kscH, view.width - 40 * _kscW, 40 * _kscH)];
    _phoneTextField.placeholder = @"请输入手机号码";
    _phoneTextField.font = [UIFont systemFontOfSize:20];
    _phoneTextField.delegate =self;
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [_phoneTextField addTarget:self action:@selector(phoneTextChange:) forControlEvents:UIControlEventEditingChanged];
    
    _phoneTextField.rightView = _tongXunLuBtn;
    _phoneTextField.rightViewMode = UITextFieldViewModeAlways;
    
    if ([[self defaultChongZhiPhone]count] != 0) {
        _phoneTextField.text = [[self defaultChongZhiPhone]firstObject];
    }
    
    [view addSubview:_phoneTextField];
    guiShuDiLabel = [[UILabel alloc]initWithFrame:CGRectMake(_phoneTextField.left, _phoneTextField.bottom, _phoneTextField.width, 20 * _kscH)];
    guiShuDiLabel.textColor = ZONGCOLOR;
    guiShuDiLabel.font = [UIFont systemFontOfSize:11];
    [view addSubview:guiShuDiLabel];
    
    hufFei = [[UILabel alloc]initWithFrame:CGRectMake(20 * _kscW, view.bottom + 20 * _kscH, KScreenWidth, 20 * _kscH)];
    hufFei.text = @"充话费";
    hufFei.font = [UIFont systemFontOfSize:13];
    hufFei.textColor = MaintextColor;
    [_myScorllview addSubview:hufFei];
    
//    liuLiang = [[UILabel alloc]initWithFrame:CGRectMake(20 * _kscW, hufFei.bottom + (_chongZhiMZAry.count%3==/* DISABLES CODE */ (0)?_chongZhiMZAry.count/3:_chongZhiMZAry.count/3+1) * 80 * _kscH, KreenWidth, 20 * _kscH)];
//    liuLiang.text = @"充流量";
//    liuLiang.font = [UIFont systemFontOfSize:13];
//    liuLiang.textColor = DuoBaoZiHuiSe;
//    [_myScorllview addSubview:liuLiang];
    
    [self drawHuaFeiJinE];
    
//    liuLiangbtn = [[UIButton alloc]initWithFrame:CGRectMake(20 * _kscW  * _kscW, liuLiang.bottom , 260/3 * _kscW, 70 * _kscH)];
//    liuLiangbtn.layer.cornerRadius = 3*_kscH;
//    liuLiangbtn.layer.borderColor = [ZONGCOLOR CGColor];
//    liuLiangbtn.layer.borderWidth = 1* _kscH;
//    [liuLiangbtn setTitle:@"选流量包" forState:UIControlStateNormal];
//    [liuLiangbtn setTitleColor:ZONGCOLOR forState:UIControlStateNormal];
//    [liuLiangbtn addTarget:self action:@selector(liuLiangChongZhi) forControlEvents:UIControlEventTouchUpInside];
//    
//    [_myScorllview addSubview:liuLiangbtn];
    
    
    _myScorllview.contentSize = CGSizeMake(KScreenWidth, liuLiangbtn.bottom+ 20 * _kscH);
    //    CommonDigest
    
    CGFloat height = 0;
    if((_recordDataAry.count + 1) * 30 * _kscH > 180 * _kscH){
        height = 200 * _kscH;
    }else{
        height = (_recordDataAry.count + 1) * 30 * _kscH;
    }
    
    _recordTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, view.bottom + 5 * _kscH, KScreenWidth, height)];
    _recordTableView.delegate = self;
    _recordTableView.dataSource = self;
    _recordTableView.hidden = YES;
    [_myScorllview addSubview:_recordTableView];
    
    _hfczView = [[JieSuanView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    _hfczView.delegate = self;
    
    [self.view addSubview:_hfczView];
}

//通讯录
-(void)contactsClicked{
    [_phoneTextField resignFirstResponder];
    AddressViewController *addressView = [[AddressViewController alloc]init];
    addressView.type = 10;
    addressView.block = ^(NSString *phoneNum){
        _phoneTextField.text = [NSString stringWithFormat:@"%@ %@ %@",[phoneNum substringToIndex:3],[phoneNum substringWithRange:NSMakeRange(3, 4)],[phoneNum substringFromIndex:7]];
        _recordTableView.hidden = YES;
        [self qinqiuMianZhiList];
        [_phoneTextField resignFirstResponder];
    };
    [self.navigationController pushViewController:addressView animated:YES];
}

-(void)drawHuaFeiJinE{
    
    for (int i = 0; i < _chongZhiMZAry.count; i++) {
        int a = i %3;
        int b =i /3;
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(20 * _kscW + a * (10+ 260/3) * _kscW, hufFei.bottom + b * 80 * _kscH , 260/3 * _kscW, 70 * _kscH)];
        
        btn.layer.cornerRadius = 3*_kscH;
        btn.layer.borderColor = [ZONGCOLOR CGColor];
        btn.layer.borderWidth = 1* _kscH;
        btn.tag = i;
        btn.backgroundColor = [UIColor whiteColor];
        [btn addTarget:self action:@selector(huaFeiChongZhi:) forControlEvents:UIControlEventTouchUpInside];
        [_myScorllview addSubview:btn];
        
        UILabel *yuanJiaLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 20 * _kscH, btn.width - 10, 20 * _kscH)];
        yuanJiaLabel.text = [_chongZhiMZAry objectAtIndex:i];
        yuanJiaLabel.textColor = ZONGCOLOR;
        yuanJiaLabel.font = [UIFont systemFontOfSize:15];
        yuanJiaLabel.textAlignment = NSTextAlignmentCenter;
        [btn addSubview:yuanJiaLabel];
        
        UILabel *xianJiaLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, yuanJiaLabel.bottom, btn.width - 10, 15 * _kscH)];
        //        xianJiaLabel.text = @"售价30.00元";
        xianJiaLabel.textColor = ZONGCOLOR;
        xianJiaLabel.font = [UIFont systemFontOfSize:12];
        xianJiaLabel.textAlignment = NSTextAlignmentCenter;
        [btn addSubview:xianJiaLabel];
        
        
        UIImageView *thImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5 * _kscW,0 , 15 * _kscW, 25 * _kscH)];
        thImageView.image = [UIImage imageNamed:@"特惠"];
        //        thImageView.backgroundColor = [UIColor redColor];
        thImageView.hidden = NO;
        
        [btn addSubview:thImageView];
        [_youHuiImageAry addObject:thImageView];
        [_nameLabelLabelAry addObject:yuanJiaLabel];
        [_detailLabelAry addObject:xianJiaLabel];
        [_huaFeiBtnAry addObject:btn];
    }
    
    [self changeBtnClolorWith:0 andAry:nil];
    if(_phoneTextField.text.length == 13){
        [self qinqiuMianZhiList];
    }
}


-(void)changeBtnClolorWith:(int)BS andAry:(NSArray *)ary{
    if (BS == 0) {//不能点
        guiShuDiLabel.textColor = [UIColor lightGrayColor];
        for (UIButton *btn in _huaFeiBtnAry) {
            btn.layer.borderColor = [LIGHTGRAYBACKCOLOR CGColor];
            btn.enabled = NO;
        }
        for (UILabel *nameLabel in _nameLabelLabelAry) {
            nameLabel.textColor = LIGHTGRAYBACKCOLOR;
        }
        for (UILabel *detailLabel in _detailLabelAry) {
            detailLabel.textColor = LIGHTGRAYBACKCOLOR;
        }
        for (UIImageView *yhImage in _youHuiImageAry){
            yhImage.hidden = YES;
        }
    }else{
        guiShuDiLabel.textColor = ZONGCOLOR;
        for (int i = 0; i < ary.count; i++) {
            NSDictionary *dic = [ary objectAtIndex:i];
            UIButton *btn = [_huaFeiBtnAry objectAtIndex:i];
            btn.layer.borderColor = [ZONGCOLOR CGColor];
            btn.enabled = YES;
            
            UILabel *nameLabel = [_nameLabelLabelAry objectAtIndex:i];
            nameLabel.text = [NSString stringWithFormat:@"%@元",[dic objectForKey:@"name"]];
            nameLabel.textColor = ZONGCOLOR;
            
            UILabel *detailLabel = [_detailLabelAry objectAtIndex:i];
            detailLabel.text = [NSString stringWithFormat:@"售价%@元",[dic objectForKey:@"money"]];
            detailLabel.textColor = ZONGCOLOR;
            if ([[dic objectForKey:@"maxFare"]intValue] == 1) {
                ((UIImageView *)[_youHuiImageAry objectAtIndex:i]).hidden = NO;
            }else{
                ((UIImageView *)[_youHuiImageAry objectAtIndex:i]).hidden = YES;
            }
        }
    }
}

//判断是否为整形：

- (BOOL)isPureInt:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    int val;
    
    return[scan scanInt:&val] && [scan isAtEnd];
    
}

-(void)phoneTextChange:(UITextField *)textfield{
    
    if ([self isPureInt:textfield.text]) {
        if (textfield.text.length == 11) {
            textfield.text = [NSString stringWithFormat:@"%@ %@ %@",[textfield.text substringToIndex:3],[textfield.text substringWithRange:NSMakeRange(3, 4)],[textfield.text substringFromIndex:7]];
        }
    }
    
    if (textfield.text.length == 13) {
        _recordTableView.hidden = YES;
        [self qinqiuMianZhiList];
        [textfield resignFirstResponder];
    }else{
        _recordTableView.hidden = NO;
        [self changeBtnClolorWith:0 andAry:nil];
    }
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    textField.text = @"";
    _recordTableView.hidden = NO;
    [self changeBtnClolorWith:0 andAry:nil];
    return YES;
}

-(void)qinqiuMianZhiList{
    NSString *phoneStr = [_phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
//    if([[self defaultLonginType]intValue] == 1){//手机用户
//        [requestDic setObject:[self defaultUserId] forKey:@"_uid"];
     [requestDic setObject:@"55ff5ad6acada714685079dd" forKey:@"_uid"];
//    }
    [requestDic setObject:@"HF" forKey:@"code"];
    [requestDic setObject:phoneStr forKey:@"mobileNo"];
    //走接口
    [TCUtils showLoadingWithView:self.view];
    
    [NetWorking NEWjieKouQingQiu:QM_HFMZJK ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error){
        [TCUtils hiddonLoadingWithView:self.view];
        if (!error) {
            if ([[dic objectForKey:Code]intValue] == 200) {
                
                _phoneInfo = [[dic objectForKey:Response]objectForKey:@"phoneInfo"];
                guiShuDiLabel.text = [NSString stringWithFormat:@"%@ %@",[_phoneInfo  objectForKey:@"province"],[_phoneInfo  objectForKey:@"operator"]];
                _detailMoneyAry = [[dic objectForKey:Response]objectForKey:@"fares"];
                [self changeBtnClolorWith:1 andAry:[[dic objectForKey:Response]objectForKey:@"fares"]];
            }
        }
    }];
}

#pragma mark table数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _recordDataAry.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recordCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"recordCell"];
    }
    cell.textLabel.text = [_recordDataAry objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30 * _kscH;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIButton *sectionBakView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30 * _kscH)];
    
    sectionBakView.backgroundColor = [UIColor whiteColor];
    [sectionBakView setTitle:@"清空历史记录" forState:UIControlStateNormal];
    [sectionBakView addTarget:self action:@selector(clearRecrod) forControlEvents:UIControlEventTouchUpInside];
    [sectionBakView setTitleColor:ZONGCOLOR forState:UIControlStateNormal];
    
    UILabel *fenGeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
    fenGeLabel.backgroundColor = LIGHTGRAYBACKCOLOR;
    [sectionBakView addSubview:fenGeLabel];
    return sectionBakView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
    _phoneTextField.text = [_recordDataAry objectAtIndex:indexPath.row];
    tableView.hidden = YES;
    [self qinqiuMianZhiList];
    [_phoneTextField resignFirstResponder];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30 * _kscH;
}

//清空历史记录
-(void)clearRecrod{
    
    [TCUtils showAlertViewWithTitle:@"提示" message:@"确定清空历史记录" delegate:self cancel:@"取消" other:@"确定" tag:411];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (![string isEqual:@""]) {
        
        if (textField.text.length == 3) {
            textField.text = [NSString stringWithFormat:@"%@ ",textField.text];
        }
        if (textField.text.length == 8) {
            textField.text = [NSString stringWithFormat:@"%@ ",textField.text];
        }
        
        if (textField.text.length== 13) {
            
            [textField resignFirstResponder];
            return YES;
        }else if(textField.text.length > 13){
            return NO;
        }
    }
    return YES;
}
//话费
-(void)huaFeiChongZhi:(UIButton *)sender{
    _biaoJi = (int)sender.tag;
    NSString *phoneStr = [_phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (phoneStr.length == 11) {
        _shouJiHaoYouwu.hidden = YES;
        
        NSString *jiaGe = [NSString stringWithFormat:@"%.2f",[[[_detailMoneyAry objectAtIndex:_biaoJi]objectForKey:@"name"]floatValue]];
        
        
        NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
        [requestDic setObject:phoneStr forKey:@"mobileNo"];
        [requestDic setObject:jiaGe forKey:@"rechargeAmount"];
        
        [NetWorking NEWjieKouQingQiu:QM_CZ_SJDCPCX ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error){
            if (!error) {
                
                if ([[dic objectForKey:Code]intValue] == 200) {
                    
                    _mobileItem = [QM_MobileItem objectFromDictionary:[dic objectForKey:Response]];
                    
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"确定给手机号%@充值吗?",phoneStr] message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                    
                    alertView.tag = 120;
                    [alertView show];
                }else{
                    [TCUtils showAlertViewWithTitle:@"提示" message:[dic objectForKey:Msg] delegate:nil cancel:@"关闭" other:nil tag:0];
                }
            }
            NSLog(@"%@",[dic objectForKey:Msg]);
        }];
        
        //        NSString *orderStr = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
        
        //        NSString *str = [self md5:[NSString stringWithFormat:@"%@%@%@%@%@",JuHtOpenId,PhoneChongZhiAppKey,phoneStr,@"20",orderStr]];
        
        //        NSLog( @"_____%@,++%d",str,str.length);
        //        NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
        //        [requestDic setObject:str forKey:@"sign"];
        //        [requestDic setObject:phoneStr forKey:@"phoneno"];
        //        [requestDic setObject:@"20" forKey:@"cardnum"];
        //        [requestDic setObject:orderStr forKey:@"orderid"];
        //        [requestDic setObject:PhoneChongZhiAppKey forKey:@"key"];
        //
        //        [NetWorking shouJiHuaFeiChongZhi:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        //
        //            if (!error) {
        //                if ([[dic objectForKey:@"error_code"]intValue] == 0) {
        //                    NSLog(@"充值结果%@",[dic objectForKey:@"reason"]);
        //                    NSMutableArray *ary = [NSMutableArray arrayWithArray:[self defaultChongZhiPhone]];
        //                    [ary insertObject:_phoneTextField.text atIndex:0];
        //                    [self defaultSaveChongZhiPhone:ary];
        //                }
        //            }
        //        }];
        //
    }else{
        _shouJiHaoYouwu.hidden = NO;
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView.tag == 120) {
//        if ([self jianCeLogin]) {
        
            if (buttonIndex == 1) {
                
                float yuanJinE = [[[_detailMoneyAry objectAtIndex:_biaoJi]objectForKey:@"money"]floatValue];
                _hfczView.jELabel.text = [NSString stringWithFormat:@"￥%.2f",yuanJinE];
                
                [_hfczView show];

                
            }
//        }
    }else if(alertView.tag == 411){
        
        if (buttonIndex == 1) {
            [self defaultSaveChongZhiPhone:@[]];
            _recordDataAry = @[];
            _recordTableView.frame = CGRectMake(0, _recordTableView.frame.origin.y, KScreenWidth, (_recordDataAry.count) * 30 * _kscH);
            [_recordTableView reloadData];
        }
    }else if(alertView.tag == 99999){
        if (buttonIndex == 1) {
//            NewLoginViewController *lonView = [[NewLoginViewController alloc] init];
//            [self.navigationController pushViewController:lonView animated:YES];
        }
        
    }
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
}

-(void)liuLiangChongZhi{
    if ([[_phoneTextField.text stringByReplacingOccurrencesOfString:@" "withString:@""]length] ==11) {
        _shouJiHaoYouwu.hidden = YES;
//        LiuLiangChongZhiViewController *liuLiangView = [[LiuLiangChongZhiViewController alloc]init];
//        liuLiangView.phoneStr = _phoneTextField.text;
//        [self.navigationController pushViewController:liuLiangView animated:YES];
    }else{
        _shouJiHaoYouwu.hidden = NO;
    }
}


-(void)huaFeiChongZhiChengGong:(NSNotification *)notfication{
    
    NSDictionary *dic = [notfication object];
    
    if ([[dic objectForKey:@"jieGuo"] isEqualToString:@"CG"]) {
        //        [self zhiFuChengGong];
        [TCUtils showAlertViewWithTitle:nil message:@"支付成功!" delegate:nil cancel:@"确定" other:nil tag:0];
        //付款成功
        NSMutableArray *ary = [NSMutableArray arrayWithArray:[self defaultChongZhiPhone]];
        for (int i = 0; i < ary.count; i ++) {
            NSString *str = [ary objectAtIndex:i];
            if([str isEqualToString:_phoneTextField.text]){
                [ary removeObjectAtIndex:i];
                break;
            }
        }
        
        [ary insertObject:_phoneTextField.text atIndex:0];
        [self defaultSaveChongZhiPhone:ary];
        
    }else{
        //        [self changeInfo];
        [TCUtils showAlertViewWithTitle:nil message:@"支付失败!" delegate:nil cancel:@"确定" other:nil tag:0];
    }
    
}

#pragma mark 话费充值优惠View delegate
-(void)huaFeiZFTypeChose:(UIButton *)sender{
    
    [_hfczView viewHidden];
    
    NSMutableDictionary *requEstDic = [[NSMutableDictionary alloc]init];
    [requEstDic setObject:_mobileItem.itemId forKey:@"itemId"];
    [requEstDic setObject:_mobileItem.mobileNo forKey:@"mobileNo"];
    [requEstDic setObject:_mobileItem.rechargeAmount forKey:@"rechargeAmount"];
    //            requEstDic setObject:_mobileItem.outerTid forKey:@"outerTid"
    [requEstDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    
    [NetWorking NEWjieKouQingQiu:QM_CZ_SCDD ANDIP:IP And:requEstDic anBlock:^(NSDictionary *dic, NSError *error) {
        
        if (!error) {
            if ([[[dic objectForKey:RESULT]objectForKey:@"status"]intValue] == 1) {
                _chongZhiDic = [[dic objectForKey:RESULT] objectForKey:@"data"];
                
                NSString *jiaGe = [NSString stringWithFormat:@"%.2f",[[[_detailMoneyAry objectAtIndex:_biaoJi]objectForKey:@"money"]floatValue]];
                
                NSString *xiaoFei = [jiaGe stringByReplacingOccurrencesOfString:@"." withString:@""];//小数点上传有问题,特此变为无.
                
//                [self defaultSaveZhiFuType:@"51"];//51:手机话费充值
                if (sender.tag == 1) {//支付宝
                    
//                    [self zhiFuZhiFuBaoWith:[_chongZhiDic objectForKey:@"billId"] and:@"庭树生活手机话费充值" and:@"庭树生活手机费充值" and:[jiaGe floatValue]  andType:@"51" andCount:xiaoFei andHuiDiaoZT:1 anBlack:^(NSDictionary *resultDic) {
//                        
//                        if ([[resultDic valueForKey:@"resultStatus"] isEqualToString:@"9000"]) {
//                            
//                            NSDictionary *jieGuoDic = [TCUtils dictionaryFromJson:[resultDic objectForKey:@"result"]];
//                            
//                            //将解析得到的内容存放字典中，编码格式为UTF8，防止取值的时候发生乱码
//                            
//                            
//                            //                     [self postApliResult];
//                            
//                            NSLog(@"支付成功 %@",jieGuoDic);
//                            //                [self jieDanCaoZuo];
//                            [TCUtils showAlertViewWithTitle:@"提示" message:@"支付成功" delegate:nil cancel:@"确定" other:nil tag:0];
                            NSMutableArray *ary = [NSMutableArray arrayWithArray:[self defaultChongZhiPhone]];
                            
                            for (int i = 0; i < ary.count; i ++) {
                                NSString *str = [ary objectAtIndex:i];
                                if([str isEqualToString:_phoneTextField.text]){
                                    [ary removeObjectAtIndex:i];
                                    break;
                                }
                            }
                            
                            
                            [ary insertObject:_phoneTextField.text atIndex:0];
                            [self defaultSaveChongZhiPhone:ary];
                            
                            [self.navigationController popToRootViewControllerAnimated:YES];
//                        }else{
//                            [TCUtils showAlertViewWithTitle:@"提示" message:@"支付失败" delegate:nil cancel:@"确定" other:nil tag:0];
//                        }
//                    }];
                    
                }else if(sender.tag == 2){//微信
                    //        [self returnDinDanNumWithType:@"4" andIdStr:_yuyueInfo._id andPrice:_otherTextField.text];
                    
                    //下一个版本重点修改,jiaGe改成xiaoFei;  元变分  //因为支付宝订单号不可以有小数点
                    //不能直接传金额价钱
                    
//                    NSString *idStr = [self returnDinDanNumWithType:@"51" andIdStr:[_chongZhiDic objectForKey:@"billId"] andPrice:xiaoFei];
//                    
//                    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
//                    
//                    [requestDic setObject:idStr forKey:@"attach"];
//                    [requestDic setObject:[self defaultLoadUserName] forKey:@"username"];
//                    
//                    [NetWorking JiaMijieKouQingQiu:wxzf_CSAPI And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
//                        
//                        if(!error){
//                            if([[dic objectForKey:Code]intValue] == 200){
//                                [self sendPay_demoWith:[dic objectForKey:Response]];
//                            }
//                        }
//                    }];
//                    
//                }else if(sender.tag == 0){//余额
//                    
//                    NSString *houPinJieCS;
//                    
//                    NSString *shijiId = [[_detailMoneyAry objectAtIndex:_biaoJi]objectForKey:@"_id"];
//                    
//                    //uid_51_千米产品id_0_用户选择优惠id,手机类型,uid
//                    
//                    if([[self defaultLonginType]intValue] == 1){//手机用户
//                        
//                        houPinJieCS = [NSString stringWithFormat:@"%@,%@,%@",shijiId,[self returenShouJiLEixing:[_phoneInfo  objectForKey:@"operator"]],[self defaultUserId]];
//                    }else{
//                        houPinJieCS = [NSString stringWithFormat:@"%@,%@",shijiId,[self returenShouJiLEixing:[_phoneInfo  objectForKey:@"operator"]]];
//                    }
//                    
//                    NSString *TradeNoStr = [self returnDinDanNumWithType:@"51" andIdStr:[_chongZhiDic objectForKey:@"billId"] andPrice:@"0"];
//                    
//                    NSString *shangChuanStr = [NSString stringWithFormat:@"%@_%@",TradeNoStr,houPinJieCS];
//                    
//                    [self yueEZhiFuWithTradeNo:shangChuanStr andYuEBJ:1 AndBlack:^(NSDictionary *dic, NSError *error) {
//                        if (!error) {
//                            if ([[dic objectForKey:Code]intValue] == 200) {
//                                [TCUtils showAlertViewWithTitle:@"提示" message:@"支付成功" delegate:nil cancel:@"确定" other:nil tag:0];
//                                NSMutableArray *ary = [NSMutableArray arrayWithArray:[self defaultChongZhiPhone]];
//                                
//                                for (int i = 0; i < ary.count; i ++) {
//                                    NSString *str = [ary objectAtIndex:i];
//                                    if([str isEqualToString:_phoneTextField.text]){
//                                        [ary removeObjectAtIndex:i];
//                                        break;
//                                    }
//                                }
//                                
//                                [ary insertObject:_phoneTextField.text atIndex:0];
//                                [self defaultSaveChongZhiPhone:ary];
//                                
//                                [self.navigationController popToRootViewControllerAnimated:YES];
//                            }else{
//                                [TCUtils showAlertViewWithTitle:@"提示" message:[dic objectForKey:Msg] delegate:nil cancel:@"确定" other:nil tag:0];
//                            }
//                            
//                        }
//                        
//                    }];
//                    
                }
            }
        }
    }];
}

-(NSString *)returenShouJiLEixing:(NSString*)str{
    if ([str isEqualToString:@"移动"]) {
        return @"YD";
    }else if([str isEqualToString:@"联通"]){
        return @"LT";
    }else if([str isEqualToString:@"电信"]){
        return @"DX";
    }else{
        return @"XN";
    }
    
}

-(void)clearBtnClicked:(UIButton *)sender{
    [_hfczView viewHidden];
}

-(void)yuESwitchClicked:(UISwitch*)mySwitch{
    
    
    if (mySwitch.isOn) {
        NSString *jiaGe = [NSString stringWithFormat:@"%.2f",[[[_detailMoneyAry objectAtIndex:_biaoJi]objectForKey:@"money"]floatValue]];
        _hfczView.jELabel.text = [NSString stringWithFormat:@"￥%.2f",[jiaGe floatValue]-_yuEJianMoney];
    }else{
        NSString *jiaGe = [NSString stringWithFormat:@"%.2f",[[[_detailMoneyAry objectAtIndex:_biaoJi]objectForKey:@"money"]floatValue]];
        _hfczView.jELabel.text = [NSString stringWithFormat:@"￥%@",jiaGe];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
